﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace Experiments2D.Bublz
{
    public class Level
    {
        protected Random m_random;

        protected int m_boardMoveInterval;
        public int BoardMoveInterval
        {
            get { return m_boardMoveInterval; }
        }
        protected int m_starObjective;
        public int StarObjective
        {
            get { return m_starObjective; }
        }
        protected int m_nbBubblesKind;
        public int NbBubblesKind
        {
            get { return m_nbBubblesKind; }
        }

        protected int m_specialBubbleCounter = 0;
        protected int m_specialBubbleFrequency;
        public int SpecialbubbleFrrquency
        {
            get { return m_specialBubbleFrequency; }
        }

        protected int m_deadBubbleCounter = 0;
        protected int m_deadBubbleFrequency;
        public int DeadBubbleFrequency
        {
            get { return m_deadBubbleFrequency; }
        }

        protected int m_levelNum;
        public int LevelNum
        {
            get { return m_levelNum; }
        }

        protected int m_minBubbleCountToMoveDown;
        public int MinBubbleCountToMoveDown
        {
            get { return m_minBubbleCountToMoveDown; }
        }


        public Level(int levelNum, int boardMoveInterval, int starObjective, int nbBubblesKind, int specialBubbleFrequency, int deadBubbleFrequency, int minBubbleCountToMoveDown)
        {
            m_levelNum = levelNum;
            m_boardMoveInterval = boardMoveInterval;
            m_starObjective = starObjective;
            m_nbBubblesKind = nbBubblesKind;
            m_specialBubbleFrequency = specialBubbleFrequency;
            m_deadBubbleFrequency = deadBubbleFrequency;
            m_minBubbleCountToMoveDown = minBubbleCountToMoveDown;
            m_random = new Random();
        }

        public void GetBubble(out int kind, out SpecialBubbleKind bonusKind,out bool deadBubble)
        {
            m_specialBubbleCounter++;
            m_deadBubbleCounter++;
            bool specialBubble = false;
            deadBubble = false;
            kind = m_random.Next(0, m_nbBubblesKind);
            if (m_specialBubbleCounter == m_specialBubbleFrequency)
            {
                specialBubble = (m_random.Next(0, 2) == 1) ? true : false;
                m_specialBubbleCounter = 0;
            }

            if (m_deadBubbleCounter == m_deadBubbleFrequency)
            {
                deadBubble = (m_random.Next(0, 2) == 1) ? true : false;                
                m_deadBubbleCounter = 0;
            }

            if (specialBubble)
            {   
                bonusKind = SpecialBubbleKind.Star;
            }
            else
            {   
                bonusKind = SpecialBubbleKind.None;
            }
        }
    }

    public class LevelManager
    {
        private static LevelManager m_instance = new LevelManager ();
        private static object m_obj = new object();
        public static LevelManager Instance
        {
            get
            {
                lock (m_obj)
                {
                    return m_instance;
                }
            }
        }

        private List<Level> m_levels;
        public List<Level> Levels
        {
            get { return m_levels; }
        }

        private LevelManager()
        {
            m_levels = new List<Level>();
            // level definition
            Level level1 = new Level(1, 30000, 15, 4, 15, 100, 40);
            Level level2 = new Level(2, 25000, 20, 5, 15, 100, 50);
            Level level3 = new Level(3, 20000, 25, 6, 20, 50, 60);
            Level level4 = new Level(4, 17000, 30, 7, 20, 20, 60);
            Level level5 = new Level(5, 16000, 35, 7, 25, 15, 60);
            Level level6= new Level(6, 15000, 40, 7, 25, 15, 60);

            m_levels.Add(level1);
            m_levels.Add(level2);
            m_levels.Add(level3);
            m_levels.Add(level4);
            m_levels.Add(level5);
            m_levels.Add(level6);

        }
    }


}
